package com.dodoke.dao.impl;

import com.dodoke.dao.UserDao;
import com.dodoke.entity.Role;
import com.dodoke.entity.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;


public class UserDaoImpl implements UserDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 查询所有用户列表
     * @return
     */
    @Override
    public List<User> findAllList() {
        List<User> userList = jdbcTemplate.query("select id," +
                "username,email," +
                "phone_num AS phoneNum " +
                "from sys_user",
                new BeanPropertyRowMapper<>(User.class));
        return userList;
    }

    /**
     * 保存用户
     * @param user
     * @return
     */
    @Override
    public Integer addUser(User user) {
        // 1.创建PreparedStatementCreator对象
        PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement preparedStatement = connection.prepareStatement("insert into sys_user(username,password,email,phone_num) values(?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
                preparedStatement.setString(1, user.getUsername());
                preparedStatement.setString(2, user.getPassword());
                preparedStatement.setString(3, user.getEmail());
                preparedStatement.setString(4, user.getPhoneNum());
                return preparedStatement;
            }
        };
        // 2.创建keyHolder
        GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(preparedStatementCreator,generatedKeyHolder);
        // 获取主键
        Integer userId = generatedKeyHolder.getKey().intValue();
        return userId;
    }

    /**
     * 保存用户角色关联关系
     * @param userId
     * @param roleId
     * @return
     */
    @Override
    public Integer addUserRole(Integer userId, Integer roleId) {
        Integer i = jdbcTemplate.update("insert into sys_user_role(user_id,role_id) values(?,?)", userId, roleId);
        return i;
    }

    /**
     * 根据用户id删除
     * @param userId
     */
    @Override
    public void deleteByUserId(Integer userId) {
        jdbcTemplate.update("delete from sys_user where id = ?",userId);
    }

    /**
     * 根据用户id删除用户和角色的关联关系
     * @param userId
     */
    @Override
    public void deleteRoleByUserId(Integer userId) {
        jdbcTemplate.update("delete from sys_user_role where user_id = ?",userId);
    }

    @Override
    public User findInfoByUsernameAndPassword(String username, String password) throws EmptyResultDataAccessException {
        User user = jdbcTemplate.queryForObject("select id from sys_user where username = ? and password = ?",
                new BeanPropertyRowMapper<User>(User.class),
                username, password);
        return user;
    }

}
